class Solution:
    def isMatch(self, s, p) :
        n = len(s)
        m = len(p)
        l = 0
        if p == ".*":
            return True
        if p[0] == "*":
            p = p[1:]
        if "*" not in p:
            if m != n:
                return False
            else:
                for i in s:
                    if s[i] == p[i] or p[i] == ".":
                        pass
                    else:

                        return False
                return True
        if p[0] == "*":
            p = p[1:]

        elif p[-1] == "*":
            i =0
            r =0
            while i < n:

                if s[i] == p[r] or p[r] == ".":
                    l +=1
                    r+=1
                    pass
                elif p[r] == "*" :
                    if s[i] == s[i-1] :
                        i +=1
                    else:
                        return False
            return True

        else:
            while l < n:
                r = l
                if s[l] == p[r]:
                    l += 1
                    r += 1
                    pass
                elif p[r] == "*":
                    try:
                        while s[l] == s[l - 1] and l < n :
                            l += 1

                    except:
                        return True

                    if s[l] ==p[r] :
                        l +=1
                        r +=1
                        pass
                    else:
                        return False
                else:
                    return False


ss = Solution()
s = "aa"
p = "a*"

print(ss.isMatch(s, p))
